<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>天天生鲜-商品详情</title>
    <link rel="stylesheet" type="text/css" th:href="@{/css/reset.css}" href="../static/css/reset.css">
    <link rel="stylesheet" type="text/css" th:href="@{/css/main.css}" href="../static/css/main.css">

</head>
<body>
<div class="header_con" th:include="inner::header"></div>
<div class="search_bar clearfix" th:include="inner::search"></div>

<div class="navbar_con">
    <div class="navbar clearfix">
        <div class="subnav_con fl">
            <h1>全部商品分类</h1>
            <span></span>
            <ul class="subnav">
                <li><a href="#" th:href="@{/list}" class="fruit">新鲜水果</a></li>
                <li><a href="#" th:href="@{/list}" class="seafood">海鲜水产</a></li>
                <li><a href="#" th:href="@{/list}" class="meet">猪牛羊肉</a></li>
                <li><a href="#" th:href="@{/list}" class="egg">禽类蛋品</a></li>
                <li><a href="#" th:href="@{/list}" class="vegetables">新鲜蔬菜</a></li>
                <li><a href="#" th:href="@{/list}" class="ice">速冻食品</a></li>
            </ul>
        </div>
        <ul class="navlist fl">
            <li><a href="#" th:href="@{/}">首页</a></li>
            <li class="interval">|</li>
            <li><a href="#" th:href="@{/list}">生鲜</a></li>
            <li class="interval">|</li>
            <li><a href="#" th:href="@{/list}">抽奖</a></li>
        </ul>
    </div>
</div>

<div class="breadcrumb">
    <a href="#">全部分类</a>
    <span>></span>
    <a href="#">新鲜水果</a>
    <span>></span>
    <a href="#">商品详情</a>
</div>

<div class="goods_detail_con clearfix">
    <div class="goods_detail_pic fl"><img src="../static/images/goods_detail.jpg"
                                          th:src="${product.imageUrl}"></div>

    <div class="goods_detail_list fr">
        <h3 th:text="${product.name}">大兴大棚草莓</h3>
        <p th:text="${product.description}">
            草莓浆果柔软多汁，味美爽口，适合速冻保鲜贮藏。草莓速冻后，可以保持原有的色、香、味，既便于贮藏，又便于外销。</p>
        <div class="prize_bar">
            <span class="show_pirze">¥<em th:text="${product.price}">16.80</em></span>
            <span class="show_unit">单  位：<span th:text="${product.unit}">500g</span></span>
        </div>
        <div class="goods_num clearfix">
            <div class="num_name fl">数 量：</div>
            <div class="num_add fl">
                <a href="javascript:;" class="add fr">+</a>
                <input type="text" class="num_show fl" id="cartCount" value="1">
                <a href="javascript:;" class="minus fr">-</a>
            </div>
        </div>
        <div class="total">库存：<span th:text="${product.stock}">9999</span></div>
        <div class="total">总价：<em id="total" th:text="${product.price}">16.80</em> 元</div>
        <div class="operate_btn">
            <a href="javascript:;" class="buy_btn" id="buy">立即购买</a>
            <a href="javascript:;" class="add_cart" id="add_cart">加入购物车</a>
        </div>
    </div>
</div>

<div class="main_wrap clearfix">
    <div class="l_wrap fl clearfix">
        <div class="new_goods">
            <h3>新品推荐</h3>
            <ul>
                <li>
                    <a href="#"><img src="../static/images/goods/goods001.jpg"
                                     th:src="@{/images/goods/goods001.jpg}"></a>
                    <h4><a href="#">进口柠檬</a></h4>
                    <div class="prize">￥3.90</div>
                </li>
                <li>
                    <a href="#"><img src="../static/images/goods/goods002.jpg"
                                     th:src="@{/images/goods/goods002.jpg}"></a>
                    <h4><a href="#">玫瑰香葡萄</a></h4>
                    <div class="prize">￥16.80</div>
                </li>
            </ul>
        </div>
    </div>

    <div class="r_wrap fr clearfix">
        <ul class="detail_tab clearfix">
            <li class="active">商品介绍</li>
            <li>评论</li>
        </ul>

        <div class="tab_content">
            <th:block th:each="detail : ${product.details}">
                <dl>
                    <dd>
                        <img src="../static/images/goods/goods003.jpg"
                             th:src="${detail.imageUrl}">
                    </dd>
                </dl>
                <dl>
                    <dd th:text="${detail.description}">草莓采摘园位于北京大兴区 庞各庄镇四各庄村
                        ，每年1月-6月面向北京以及周围城市提供新鲜草莓采摘和精品礼盒装草莓，草莓品种多样丰富，个大香甜。所有草莓均严格按照有机标准培育，不使用任何化肥和农药。草莓在采摘期间免洗可以直接食用。欢迎喜欢草莓的市民前来采摘，也欢迎各大单位选购精品有机草莓礼盒，有机草莓礼盒是亲朋馈赠、福利送礼的最佳选择。
                    </dd>
                </dl>
            </th:block>
        </div>
    </div>
</div>

<div th:include="inner::footer" class="footer"></div>

<div class="add_jump"></div>

<script type="text/javascript" src="../static/js/jquery.min.js" th:src="@{/js/jquery.min.js}"></script>
<script type="text/javascript" th:inline="javascript">  /*<![CDATA[*/
var user = [[${session.user}]];
console.log('current user', user)
/*]]>*/
</script>
<script type="text/javascript">
    var $add_x = $('#add_cart').offset().top;
    var $add_y = $('#add_cart').offset().left;

    var $to_x = $('#show_count').offset().top;
    var $to_y = $('#show_count').offset().left;

    $(".add_jump").css({'left': $add_y + 80, 'top': $add_x + 10, 'display': 'block'})


    let cartCount = $('#cartCount').val();

    $('.add').click(function () {
        cartCount++;
        if(cartCount > [[${product.stock}]]) {
            alert('库存不足')
            return;
        }
        $('#cartCount').val(cartCount)
        $('#total').text((cartCount * [[${product.price}]]).toFixed(2))
    })

    $('.minus').click(function () {
        if (cartCount >= 2) {
            cartCount--;
        }
        $('#cartCount').val(cartCount)
        $('#total').text((cartCount * [[${product.price}]]).toFixed(2))
    })

    $('#cartCount').blur(function() {
        let count = $(this).val();

        if(count > [[${product.stock}]]) {
            alert('库存不足')
            $(this).val([[${product.stock}]])
        }

        if (count < 1) {
            $(this).val(1)
        }

        $('#total').text(($(this).val() * [[${product.price}]]).toFixed(2))
    })

    $("#buy").click(function () {
        if (user == null) {
            window.location.href = '/login';
        } else {
            let obj = {
                userId: user.id,
                productId: [[${product.id}]],
                quantity: cartCount
            };
            $.ajax({
                url: '/cart/save',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(obj),
                success: function (result) {
                    if (result.code == 200) {
                        window.location.href = '/buy/'+result.data.id;
                    }
                }
            })
        }
    })

    $('#add_cart').click(function () {
        if (user == null) {
            window.location.href = '/login';
        } else {
            $(".add_jump").stop().animate({
                    'left': $to_y + 7,
                    'top': $to_x + 7
                },
                "fast", function () {
                    $(".add_jump").fadeOut('fast', function () {
                        let obj = {
                            userId: user.id,
                            productId: [[${product.id}]],
                            quantity: cartCount
                        };
                        $.ajax({
                            url: '/cart/save',
                            type: 'POST',
                            contentType: 'application/json',
                            data: JSON.stringify(obj),
                            success: function (result) {
                                if (result.code == 200) {
                                    $('#show_count').html(parseInt($('#show_count').text()) + parseInt(cartCount));
                                    $(".add_jump").css({'left': $add_y + 80, 'top': $add_x + 10, 'display': 'block'})
                                } else {
                                    alert(result.message)
                                }
                            }
                        })
                    });
                });
        }
    })
</script>

</body>
</html>